package com.zeng.plugin.util;

import com.zeng.plugin.entity.TableEntity;

import com.zeng.plugin.intefaceHandle.ParseHandle;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CreateFactory {


    public static void createMapper(String MapperPackaeg
            , String mapperJavaPath
            , List<TableEntity> tableEntities
            , String extendPackage
            , FreeMarkUtil freeMarkUtil) {
        try {
            File file = new File(mapperJavaPath);
            boolean exists = file.exists();
            if(!exists){
                file.mkdirs();
            }

            for (TableEntity tableEntity : tableEntities) {
                Map<String, Object> data = new HashMap<>();
                data.put("mapperPackaeg", MapperPackaeg);
                String interfacaName = tableEntity.getClassName() + "Mapper";
                data.put("intefaceName", interfacaName);
                data.put("extendPackage", extendPackage);
                data.put("entendClass", tableEntity.getClassName());
                File file1 = new File(mapperJavaPath + "\\" + interfacaName + ".java");
                if (!file1.exists()) {
                    freeMarkUtil.parse("mapper.ftl", file1, data);
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
    public static void createByTemplate(Map<String,String> mapPackage
            , String javaPath
            , List<TableEntity> tableEntities
            , FreeMarkUtil freeMarkUtil
            , String templateFile
            , ParseHandle parseHandle
            ) throws IOException {
        try {
            File file = new File(javaPath);
            boolean exists = file.exists();
            if(!exists){
                file.mkdirs();
            }
            parseHandle.parse(tableEntities,mapPackage,javaPath,templateFile,freeMarkUtil);


        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }

}

